//
// Created by Ge on 2024/3/4.
//
#include "include/CourseOne.h"
#include "include/ShareLoader.h"
#include <jni.h>
#include <GLES2/gl2.h>
#include <stdlib.h>
#include "include/LogUts.h"

#ifndef DETECTION_REGISTER_CLASS
#define DETECTION_REGISTER_CLASS "com/ziya/android/a3dmodel/nativelib/CourseOneNative"
#endif
JNINativeMethod methods[] = {
        {"drawTriangle", "()V", (void *) &drawTriangle}
};

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    JNIEnv *env;
    if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) {
        return -1;
    }
    jclass clz = env->FindClass(DETECTION_REGISTER_CLASS);
    env->RegisterNatives(clz, methods, sizeof(methods) / sizeof(JNINativeMethod));
    env->DeleteLocalRef(clz);
    return JNI_VERSION_1_6;
}

JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
    JNIEnv *env = NULL;
    if (JNI_OK != vm->GetEnv((void **) &env, JNI_VERSION_1_6)) {
        return;
    }
    jclass clz = env->FindClass(DETECTION_REGISTER_CLASS);
    env->UnregisterNatives(clz);
    env->DeleteLocalRef(clz);
}

//顶点着色器
const char *vertexShader = "attribute vec4 vPosition;\n"
                           "void main() {\n"
                           "  gl_Position = vPosition;\n"
                           "}";

//片元着色器
const char *fragmentShader = "precision mediump float;\n"
                             "void main() {\n"
                             "  gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);\n"
                             "}";
// 设置三角形顶点数据
static const GLfloat triangleVertices[] = {
        0.0f, 1.0f, 0.0f,
        -1.0f, -0.0f, 0.0f,
        1.0f, -0.0f, 0.0f
};

void drawTriangle(JNIEnv *env, jobject thiz) {
    //加载顶点着色器
    GLuint vertexShaderHandle = loadShader(GL_VERTEX_SHADER, vertexShader);
    //加载片元着色器
    GLuint fragmentShaderHandle = loadShader(GL_FRAGMENT_SHADER, fragmentShader);
    //创建着色器程序
    GLuint shaderProgramHandle = glCreateProgram();
    glAttachShader(shaderProgramHandle, vertexShaderHandle);
    glAttachShader(shaderProgramHandle, fragmentShaderHandle);
    //链接着色器程序
    glLinkProgram(shaderProgramHandle);

    // 获取着色器程序链接成功与否
    GLint linkSuccess;
    glGetProgramiv(shaderProgramHandle, GL_LINK_STATUS, &linkSuccess);
    if (linkSuccess == GL_FALSE) {
        glDeleteProgram(shaderProgramHandle);
        return;
    }
    //使用着色器程序
    glUseProgram(shaderProgramHandle);


    //获取着色器中的vPosition成员的索引值
    GLuint positionIndex = glGetAttribLocation(shaderProgramHandle, "vPosition");
    //启用顶点数据
    glEnableVertexAttribArray(positionIndex);
    //将顶点数据设置到顶点着色器中的vPosition
    glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, 0, triangleVertices);
    //绘制三角形(三个顶点)
    glDrawArrays(GL_TRIANGLES, 0, 3);
    //禁用着色器
    glDisableVertexAttribArray(positionIndex);


    glDetachShader(shaderProgramHandle, vertexShaderHandle);
    glDetachShader(shaderProgramHandle, fragmentShaderHandle);
    glDeleteShader(vertexShaderHandle);
    glDeleteShader(fragmentShaderHandle);
    glDeleteProgram(shaderProgramHandle);
}